import { ref, unref } from 'vue';
import { useType } from '../../../utils/useType';

const { isType } = useType();
export const useFormCol = (props, emits, formModel) => {
  const oldValue = ref(formModel[props.dataSource.key]);
  const isUpdate = ref(false);

  const changeValue = e => {
    emits('change', { key: props.dataSource.key, value: e });
    if (isType(e, 'Object') || isType(e, 'Array')) {
      isUpdate.value = JSON.stringify(unref(oldValue)) !== JSON.stringify(e) ? true : false;
    } else {
      isUpdate.value = unref(oldValue) !== e ? true : false;
    }
  };

  return {
    changeValue,
    isUpdate,
  };
};
